package net.simflow.swf.analyzer.gui.exec.para;

import net.simflow.swf.analyzer.exec.data.ExecParameterPkg;
import net.simflow.swf.analyzer.gui.opt.TextOperater;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class ExecGeneralComposite extends Composite {
	private final static String REGEX_INT = "[1-9]\\d*";
	private final static String REGEX_DOUBLE = "([0]{1})|([0]\\.\\d*)|([1-9]\\d*\\.?\\d*)";

	private ExecParameterPkg paraPkg;

	private Text txtNormalSpeed;
	private Text txtTotalStep;
	private Button btnHasPeak;
	private Text txtPeakSpeed;
	private Text txtPeakRangeA;
	private Text txtPeakRangeB;
	private Button btnHasValley;
	private Text txtValleySpeed;
	private Text txtValleyRangeA;
	private Text txtValleyRangeB;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ExecGeneralComposite(Composite parent, ExecParameterPkg paraPkg) {
		super(parent, SWT.NONE);
		this.paraPkg = paraPkg;
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		this.setLayout(new GridLayout(3, false));

		Label lblTotalStep = new Label(this, SWT.NONE);
		lblTotalStep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		lblTotalStep.setText("Total Step:");

		txtTotalStep = new Text(this, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtTotalStep, REGEX_INT, 4);
		GridData gd_txtTotalStep = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtTotalStep.widthHint = 79;
		txtTotalStep.setLayoutData(gd_txtTotalStep);
		txtTotalStep.setText(String.valueOf(paraPkg.getTotalStep()));

		new Label(this, SWT.NONE);

		Label lblNormalSpeed = new Label(this, SWT.NONE);
		lblNormalSpeed.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		lblNormalSpeed.setText("Normal Speed:");

		txtNormalSpeed = new Text(this, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtNormalSpeed, REGEX_DOUBLE, 8);
		GridData gd_txtNormalSpeed = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtNormalSpeed.widthHint = 79;
		txtNormalSpeed.setLayoutData(gd_txtNormalSpeed);
		txtNormalSpeed.setText(String.valueOf(paraPkg.getNormalSpeed()));

		new Label(this, SWT.NONE);

		btnHasPeak = new Button(this, SWT.CHECK);
		btnHasPeak.setSelection(this.paraPkg.hasPeak());
		btnHasPeak.setText("Has Peak:");

		Composite cmpPeak = new Composite(this, SWT.NONE);
		GridLayout gl_cmpPeak = new GridLayout(6, false);
		gl_cmpPeak.marginWidth = 0;
		gl_cmpPeak.marginHeight = 0;
		cmpPeak.setLayout(gl_cmpPeak);
		cmpPeak.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));

		Label lblPeakRange = new Label(cmpPeak, SWT.NONE);
		lblPeakRange.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblPeakRange.setText("Range");

		txtPeakRangeA = new Text(cmpPeak, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtPeakRangeA, REGEX_INT, 4);
		GridData gd_txtPeakRangeA = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtPeakRangeA.widthHint = 30;
		txtPeakRangeA.setLayoutData(gd_txtPeakRangeA);
		txtPeakRangeA.setText(String.valueOf(this.paraPkg.getPeakA()));

		Label lblPeakTo = new Label(cmpPeak, SWT.NONE);
		lblPeakTo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblPeakTo.setText("-");

		txtPeakRangeB = new Text(cmpPeak, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtPeakRangeB, REGEX_INT, 4);
		GridData gd_txtPeakRangeB = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtPeakRangeB.widthHint = 30;
		txtPeakRangeB.setLayoutData(gd_txtPeakRangeB);
		txtPeakRangeB.setText(String.valueOf(this.paraPkg.getPeakB()));

		Label lblPeakSpeed = new Label(cmpPeak, SWT.NONE);
		lblPeakSpeed.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		lblPeakSpeed.setText("Speed");

		txtPeakSpeed = new Text(cmpPeak, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtPeakSpeed, REGEX_DOUBLE, 8);
		GridData gd_txtPeakSpeed = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtPeakSpeed.widthHint = 79;
		txtPeakSpeed.setLayoutData(gd_txtPeakSpeed);
		txtPeakSpeed.setText(String.valueOf(this.paraPkg.getPeakSpeed()));

		btnHasValley = new Button(this, SWT.CHECK);
		btnHasValley.setSelection(this.paraPkg.hasValley());
		btnHasValley.setText("Has Valley:");

		Composite cmpValley = new Composite(this, SWT.NONE);
		GridLayout gl_cmpValley = new GridLayout(6, false);
		gl_cmpValley.marginWidth = 0;
		gl_cmpValley.marginHeight = 0;
		cmpValley.setLayout(gl_cmpValley);
		cmpValley.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				2, 1));

		Label lblValleyRange = new Label(cmpValley, SWT.NONE);
		lblValleyRange.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblValleyRange.setText("Range");

		txtValleyRangeA = new Text(cmpValley, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtValleyRangeA, REGEX_INT, 4);
		GridData gd_txtValleyRangeA = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtValleyRangeA.widthHint = 30;
		txtValleyRangeA.setLayoutData(gd_txtValleyRangeA);
		txtValleyRangeA.setText(String.valueOf(this.paraPkg.getValleyA()));

		Label lblValleyTo = new Label(cmpValley, SWT.NONE);
		lblValleyTo.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		lblValleyTo.setText("-");

		txtValleyRangeB = new Text(cmpValley, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtValleyRangeB, REGEX_INT, 4);
		GridData gd_txtValleyRangeB = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtValleyRangeB.widthHint = 30;
		txtValleyRangeB.setLayoutData(gd_txtValleyRangeB);
		txtValleyRangeB.setText(String.valueOf(this.paraPkg.getValleyB()));

		Label lblValleySpeed = new Label(cmpValley, SWT.NONE);
		lblValleySpeed.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		lblValleySpeed.setText("Speed");

		txtValleySpeed = new Text(cmpValley, SWT.BORDER | SWT.RIGHT);
		TextOperater.getInstance().addVerify(txtValleySpeed, REGEX_DOUBLE, 8);
		GridData gd_txtValleySpeed = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_txtValleySpeed.widthHint = 79;
		txtValleySpeed.setLayoutData(gd_txtValleySpeed);
		txtValleySpeed.setText(String.valueOf(this.paraPkg.getValleySpeed()));
	}

	protected void save() {
		try {
			int totalStep = Integer.valueOf(txtTotalStep.getText());
			double normalSpeed = Double.valueOf(txtNormalSpeed.getText());
			boolean hasPeak = btnHasPeak.getSelection();
			int peakA = Integer.valueOf(txtPeakRangeA.getText());
			int peakB = Integer.valueOf(txtPeakRangeB.getText());
			double peakSpeed = Double.valueOf(txtPeakSpeed.getText());
			boolean hasValley = btnHasValley.getSelection();
			int valleyA = Integer.valueOf(txtValleyRangeA.getText());
			int valleyB = Integer.valueOf(txtValleyRangeB.getText());
			double valleySpeed = Double.valueOf(txtValleySpeed.getText());

			this.paraPkg.setTotalStep(totalStep);
			this.paraPkg.setNormalSpeed(normalSpeed);
			this.paraPkg.setHasPeak(hasPeak);
			this.paraPkg.setPeakA(peakA);
			this.paraPkg.setPeakB(peakB);
			this.paraPkg.setPeakSpeed(peakSpeed);
			this.paraPkg.setHasValley(hasValley);
			this.paraPkg.setValleyA(valleyA);
			this.paraPkg.setValleyB(valleyB);
			this.paraPkg.setValleySpeed(valleySpeed);

		} catch (Exception e) {
			return;
		}
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
